{template 'common/header'}

<ul class="nav nav-tabs">
    <li class="active" ><a href="{php echo $this->createWebUrl('goods',array('op'=>'index'));}">商品列表</a></li>
    <li ><a href="{php echo $this->createWebUrl('goods', array('op'=>'add'))}">新增商品</a></li>
    <li ><a href="{php echo $this->createWebUrl('goods', array('op'=>'unit'))}">单位管理</a></li>
</ul>
<div>
    <div class="panel panel-primary">
        <div class="panel-body">
            <div class="row">
                <div class="col-xs-2">
                    <input type="text" id="name" placeholder="商品名称/助记码" value="{$name}" class="form-control" />
                </div>
                <div class="col-xs-2">
                    <select class="form-control" id="unit">
                        <option value="0">请选择商品单位</option>
                        {loop $units $u}
                        <option value="{$u['id']}" {if $unit_id==$u['id']} selected="selected" {/if}>{$u['name']}</option>
                        {/loop}
                    </select>
                </div>
                <div class="col-xs-2">
                    <input type="text" id="price" placeholder="单价" class="form-control" value="{$price}" />
                </div>
                <div class="col-xs-2">
                    <input type="text" id="remark" placeholder="备注" class="form-control" value="{$remark}" />
                </div>
                <div class="col-xs-4">
                    <button class="btn btn-primary find">搜索</button>
                    <button class="btn btn-info export_data">导出</button>
                </div>
            </div>
        </div>
    </div>
    <table class="table">
        <tr>
            <th>商品</th>
            <th>单位</th>
            <th>运费单价</th>
            <th>快捷码</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        {loop $list $l}
        <tr>
            <td>{$l['name']}</td>
            <td>{$l['unit']}</td>
            <td>
                <input type="text" value="{$l['price']}" class="form-control price" style="width: 150px;" goods_id="{$l['id']}" />

            </td>
            <td>{$l['short_name']}</td>
            <td>
                <input type="text" value="{$l['remark']}" class="form-control remark" style="width: 150px;" goods_id="{$l['id']}" />

            </td>
            <td>
                <a href="{php echo $this->createWebUrl('goods',array('op' => 'add','id' => $l['id']))}">
                    <button class="btn btn-primary">修改</button>
                </a>
                <button class="btn btn-danger del_good" goods_id="{$l['id']}">删除</button>
            </td>
        </tr>
        {/loop}
    </table>
    <div>
        {$pager}
    </div>
</div>
<script>
    $(function(){
        $(".price").blur(function(){
            var price = $(this).val();
            var goods_id = $(this).attr('goods_id');
            if(price > 0){
                var url = "{php echo $this->createWebUrl('goods',array('op' => 'change_price'))}";
                $.ajax({
                    type : "post",
                    url : url,
                    data : {'goods_id' : goods_id, 'price' : price},
                    success : function(data){
                        if(data == 111){
                            location.reload();
                        }else{
//                            alert(data);
                        }
                    }
                });
            }else{
                alert("请输入价钱！");
            }
        });
        $(".remark").on('blur',function(){
            var remark = $(this).val();
            var goods_id = $(this).attr('goods_id');
//            if(remark != ''){
                var url = "{php echo $this->createWebUrl('goods',array('op' => 'change_remark'))}";
                $.ajax({
                    type : "post",
                    url : url,
                    data : {'goods_id' : goods_id, 'remark' : remark},
                    success : function(data){
                        if(data == 111){
                            location.reload();
                        }else{
//                            alert(data);
                        }
                    }
                });
//            }else{
//                alert("请输入价钱！");
//            }
        });
       $(".find").click(function(){
           var name = $("#name").val();
           var unit = $("#unit").val();
           var price = $("#price").val();
           var remark = $("#remark").val();
           location.href="{php echo $this->createWebUrl('goods')}&name="+name+"&unit="+unit+"&price="+price+"&remark="+remark;
       });
       $(".export_data").click(function(){
           var name = $("#name").val();
           var unit = $("#unit").val();
           var price = $("#price").val();
           var remark = $("#remark").val();
           location.href="{php echo $this->createWebUrl('goods',array('op' => 'export'))}&name="+name+"&unit="+unit+"&price="+price+"&remark="+remark;
       });
       $(".del_good").click(function(){
           var goods_id = $(this).attr("goods_id");
           if(goods_id){
               if(confirm("是否确定删除？")) {
                   var url = "{php echo $this->createWebUrl('goods',array('op' => 'goods_del'))}";
                   $.ajax({
                       type: "post",
                       url: url,
                       data: {'goods_id': goods_id},
                       success: function (data) {
                           if (data == 111) {
                               alert("操作成功！");
                               location.reload();
                           } else {
//                               alert(data);
                           }
                       }
                   });
               }
           }
       });
    });
</script>


{template 'common/footer'}